// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.web.browser;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * @author jliu
 */
public class BrowserBug {

    /**
     * Show the bug about Browser, when create two or more browser on one display, the listener of the one browser will
     * be called many times.
     * 
     * See this example, I created two browser once, and the ProgressListener will be called twice. The output will be :
     * "Left Right Left Right"
     */
    public BrowserBug() {
        Display display = new Display();
        Shell shell = new Shell(display);

        shell.setLayout(new GridLayout(2, true));
        shell.setLayoutData(new GridData(GridData.FILL_BOTH));

        Composite left = new Composite(shell, SWT.NONE);
        left.setLayout(new FillLayout());
        left.setLayoutData(new GridData(GridData.FILL_BOTH));
        showBrowser(left);
        left.setData("Left");

        Composite right = new Composite(shell, SWT.NONE);
        right.setLayout(new FillLayout());
        right.setLayoutData(new GridData(GridData.FILL_BOTH));
        showBrowser(right);
        right.setData("Right");

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    /**
     * Create a browser and navigate, add ProgressListener.
     * 
     * @param composite
     */
    void showBrowser(final Composite composite) {
        Browser browser = new Browser(composite, SWT.NONE);
        browser.addProgressListener(new ProgressListener() {

            public void changed(ProgressEvent event) {
            }

            public void completed(ProgressEvent event) {
                System.out.println(composite.getData());
            }
        });

        browser.setUrl("www.google.com");
    }

    public static void main(String[] args) {
        new BrowserBug();
    }
}
